package com.trkj.base;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import com.shamanland.fonticon.FontIconDrawable;
import com.trkj.taoruiyun.R;

/**
 * Created by LiZanhong on 2015/06/24.
 */
public class BaseBackFragmentActivity extends BaseFragmentActivity {
    private Fragment contentFragment;

    public void setContentFragment(Fragment contentFragment) {
        this.contentFragment = contentFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        attachFragment();
    }

    @Override
    protected void initActionBar() {
        super.initActionBar();
        ActionBar actionBar = this.getActionBar();
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setIcon(FontIconDrawable.inflate(this, R.xml.left_arrow));
            actionBar.setTitle(getCustomerTitle());
            if(getSubCustomerTitle() != null){
                actionBar.setSubtitle(getSubCustomerTitle());
            }
        }
    }

    public String getCustomerTitle(){
        return "";
    }

    public String getSubCustomerTitle(){
        return null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            this.finish();
        }

        return super.onOptionsItemSelected(item);
    }

    protected void attachFragment(){
        if(contentFragment != null) {
            //将PreferenceFragment加载到布局文件中
            FragmentManager fm = this.getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.container, contentFragment);
            ft.commit();
        }
    }
}
